package com.ruoyi.standingbook.toilet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.standingbook.cleaning.domain.CleaningImportDetail;
import com.ruoyi.standingbook.toilet.domain.Toilet;
import com.ruoyi.standingbook.toilet.domain.ToiletImportDetail;
import com.ruoyi.standingbook.toilet.domain.bo.ToiletImportDetailBo;
import com.ruoyi.standingbook.toilet.domain.vo.ToiletImportDetailVo;
import com.ruoyi.standingbook.toilet.mapper.ToiletImportDetailMapper;
import com.ruoyi.standingbook.toilet.service.IToiletImportDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 市政公厕管养导入明细Service业务层处理
 *
 * @author cc
 * @date 2023-04-30
 */
@RequiredArgsConstructor
@Service
public class ToiletImportDetailServiceImpl implements IToiletImportDetailService {

    private final ToiletImportDetailMapper baseMapper;

    /**
     * 查询市政公厕管养导入明细
     */
    @Override
    public ToiletImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询市政公厕管养导入明细列表
     */
    @Override
    public TableDataInfo<ToiletImportDetailVo> queryPageList(ToiletImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ToiletImportDetail> lqw = buildQueryWrapper(bo);
        Page<ToiletImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询市政公厕管养导入明细列表
     */
    @Override
    public List<ToiletImportDetailVo> queryList(ToiletImportDetailBo bo) {
        LambdaQueryWrapper<ToiletImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ToiletImportDetail> buildQueryWrapper(ToiletImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ToiletImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), ToiletImportDetail::getBatchNo, bo.getBatchNo());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), ToiletImportDetail::getBatchNo, bo.getCommunity());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), ToiletImportDetail::getItemName, bo.getItemName());
        lqw.like(StringUtils.isNotBlank(bo.getToiletName()), ToiletImportDetail::getItemName, bo.getToiletName());
        lqw.between(params.get("beginDate") != null && params.get("endDate") != null,
            ToiletImportDetail::getDate ,params.get("beginDate"), params.get("endDate"));
        lqw.orderByDesc(ToiletImportDetail::getCreateTime);
        lqw.orderByDesc(ToiletImportDetail::getDate);
        return lqw;
    }

    /**
     * 新增市政公厕管养导入明细
     */
    @Override
    public Boolean insertByBo(ToiletImportDetailBo bo) {
        ToiletImportDetail add = BeanUtil.toBean(bo, ToiletImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改市政公厕管养导入明细
     */
    @Override
    public Boolean updateByBo(ToiletImportDetailBo bo) {
        ToiletImportDetail update = BeanUtil.toBean(bo, ToiletImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ToiletImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除市政公厕管养导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量插入
     */
    @Override
    public Boolean batchInsertVo(List<ToiletImportDetailVo> voList) {
        List<ToiletImportDetail> list= BeanCopyUtils.copyList(voList , ToiletImportDetail.class);
        return baseMapper.insertBatch(list);
    }
}
